package 笔试.暑期2023.腾讯音乐2022_暑期招聘;

/**
 * https://www.nowcoder.com/test/39959332/summary
 */
public class T1_Solution {
    public int minCnt(String s) {
        // 创建一个数组记录每一个位置 其右边 包含 "1" 的个数
        int len = s.length();
        int[] rightOneCount = new int[len];
        int count = 0;
        for (int i = len - 1; i >= 0; i--) {
            rightOneCount[i] = count;
            if (s.charAt(i) == '1') {
                count++;
            }
        }
        int index = 0;
        int minCount = len;
        while (index < len) {
            if (s.charAt(index) != '1') {
                index++;
            } else {// 找到左端的 1 开始更新最小操作数
                minCount = Math.min(minCount, index + rightOneCount[index]);
            }
            index++;
        }
        return minCount;
    }

    public static void main(String[] args) {
        System.out.println(new T1_Solution().minCnt("1010"));
    }
}